package com.yz.vi.commons.result;

import java.io.Serializable;

/**
 * @description:
 * @author: yutingting
 * @create: 2021-01-22 09:57
 **/
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1331134667810352183L;
    private boolean success;
    private ErrorContext errorContext;
    private T data;

    public Result() {
    }

    public Result(boolean success, ErrorContext errorContext, T data) {
        this.success = success;
        this.errorContext = errorContext;
        this.data = data;
    }

    public void addError(String errorCode, String errorMsg, String errorLocation) {
        if (this.errorContext == null) {
            this.errorContext = new ErrorContext();
        }

        this.errorContext.addError(new CommonError(errorCode, errorMsg, errorLocation));
    }

    public void addError(String errorCode, String errorMsg, String errorLocation, Throwable throwable) {
        if (this.errorContext == null) {
            this.errorContext = new ErrorContext();
        }

        this.errorContext.addError(new CommonError(errorCode, errorMsg, errorLocation, throwable));
    }

    public String getErrorCode() {
        return this.errorContext == null ? null : this.errorContext.fetchCurrentErrorCode();
    }

    public String getErrorMsg() {
        return this.errorContext == null ? null : this.errorContext.fetchCurrentErrorMsg();
    }

    public String getCode() {
        return this.errorContext == null ? null : this.errorContext.fetchCurrentErrorCode();
    }

    public String getMessage() {
        return this.errorContext == null ? null : this.errorContext.fetchCurrentErrorMsg();
    }

    public boolean isSuccess() {
        return this.success;
    }

    public ErrorContext getErrorContext() {
        return this.errorContext;
    }

    public T getData() {
        return this.data;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public void setErrorContext(ErrorContext errorContext) {
        this.errorContext = errorContext;
    }

    public void setData(T data) {
        this.data = data;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof Result)) {
            return false;
        } else {
            Result<?> other = (Result)o;
            if (!other.canEqual(this)) {
                return false;
            } else if (this.isSuccess() != other.isSuccess()) {
                return false;
            } else {
                Object this$errorContext = this.getErrorContext();
                Object other$errorContext = other.getErrorContext();
                if (this$errorContext == null) {
                    if (other$errorContext != null) {
                        return false;
                    }
                } else if (!this$errorContext.equals(other$errorContext)) {
                    return false;
                }

                Object this$data = this.getData();
                Object other$data = other.getData();
                if (this$data == null) {
                    if (other$data != null) {
                        return false;
                    }
                } else if (!this$data.equals(other$data)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(Object other) {
        return other instanceof Result;
    }

    public int hashCode() {
        int result = 1;
        result = result * 59 + (this.isSuccess() ? 79 : 97);
        Object $errorContext = this.getErrorContext();
        result = result * 59 + ($errorContext == null ? 43 : $errorContext.hashCode());
        Object $data = this.getData();
        result = result * 59 + ($data == null ? 43 : $data.hashCode());
        return result;
    }

    public String toString() {
        return "Result(success=" + this.isSuccess() + ", errorContext=" + this.getErrorContext() + ", data=" + this.getData() + ")";
    }
}
